﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.So;
using nERP.Model;
using nERP.Model.Bas;
using nERP.Model.Fin;
using nERP.Model.Po;

namespace nERP.ViewModel.So.SaleVMs
{
    public partial class SaleListVM : BasePagedListVM<Sale_View, SaleSearcher>
    {

        protected override IEnumerable<IGridColumn<Sale_View>> InitGridHeader()
        {
            return new List<GridColumn<Sale_View>>{

                this.MakeGridHeader(x => x.Sale_SaleCode).SetTitle(@Localizer["Page.销售单号"].Value),
                this.MakeGridHeader(x => x.Sale_Customer).SetTitle(@Localizer["Page.客户"].Value),
                this.MakeGridHeader(x => x.Sale_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.Sale_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.Sale_DeliveryDate).SetTitle(@Localizer["Page.交货日期"].Value),
                this.MakeGridHeader(x => x.Sale_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.Sale_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.Sale_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.Sale_TotalPrice).SetTitle(@Localizer["Page.总金额"].Value),
                this.MakeGridHeader(x => x.Sale_TotalPrice1).SetTitle(@Localizer["Page.已结金额"].Value),
                this.MakeGridHeader(x => x.Sale_TotalPrice2).SetTitle(@Localizer["Page.未结金额"].Value),
                this.MakeGridHeader(x => x.Sale_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Sale_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Sale_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Sale_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.Sale_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<Sale_View> GetSearchQuery()
        {
            var query = DC.Set<Sale>()

                .CheckContain(Searcher.SaleCode, x => x.SaleCode)
                .CheckEqual(Searcher.CustomerId, x => x.CustomerId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckBetween(Searcher.DeliveryDate?.GetStartTime(), Searcher.DeliveryDate?.GetEndTime(), x => x.DeliveryDate, includeMax: false)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.SettlementAccountId, x => x.SettlementAccountId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TotalPrice1, x => x.TotalPrice1)
                .CheckEqual(Searcher.TotalPrice2, x => x.TotalPrice2)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new Sale_View
                {
                    ID = x.ID,

                    Sale_SaleCode = x.SaleCode,
                    Sale_Customer = DC.Set<Customer>().Where(z0 => z0.CustomerCode == x.CustomerId).Select(y => y.CustomerName).FirstOrDefault(),
                    Sale_InvoicesDate = x.InvoicesDate,
                    Sale_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    Sale_DeliveryDate = x.DeliveryDate,
                    Sale_Remark = x.Remark,
                    Sale_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    Sale_Status = x.Status,
                    Sale_TotalPrice = x.TotalPrice,

                    //销售订单已结金额=收款金额-销售退货金额
                    Sale_TotalPrice1 = DC.Set<ReceivableDetail>().Where(z0 => z0.SaleId == x.SaleCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.Amount4)
                    + DC.Set<SaleReturnDetail>().Where(z0 => z0.SaleId == x.SaleCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.DiscountPrice),
                    //销售订单未结金额=订单金额-已结金额
                    Sale_TotalPrice2 = x.TotalPrice - DC.Set<ReceivableDetail>().Where(z0 => z0.SaleId == x.SaleCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.Amount4)
                    - DC.Set<SaleReturnDetail>().Where(z0 => z0.SaleId == x.SaleCode && z0.Status == dStatusTypeEnum.Reviewed).Sum(z0 => z0.DiscountPrice),


                    Sale_CreateTime = x.CreateTime,
                    Sale_UpdateTime = x.UpdateTime,
                    Sale_CreateBy = x.CreateBy,
                    Sale_UpdateBy = x.UpdateBy,
                    Sale_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class Sale_View : Sale
    {

        public string Sale_SaleCode { get; set; }
        public string Sale_Customer { get; set; }
        public DateTime? Sale_InvoicesDate { get; set; }
        public string Sale_MakeUser { get; set; }
        public DateTime? Sale_DeliveryDate { get; set; }
        public string Sale_Remark { get; set; }
        public string Sale_SettlementAccount { get; set; }
        public StatusTypeEnum? Sale_Status { get; set; }
        public decimal? Sale_TotalPrice { get; set; }
        public decimal? Sale_TotalPrice1 { get; set; }
        public decimal? Sale_TotalPrice2 { get; set; }
        public DateTime? Sale_CreateTime { get; set; }
        public DateTime? Sale_UpdateTime { get; set; }
        public string Sale_CreateBy { get; set; }
        public string Sale_UpdateBy { get; set; }
        public bool Sale_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}